//
//  E7LoadingScene.cpp
//  E7_ENGINE
//
//  Created by cc on 12-2-29.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7LoadingScene.h"

#include "E7GameManager.h"
extern CE7GameManager* g_pGameManager ;

CE7LoadingScene::CE7LoadingScene()
{
}

CE7LoadingScene::~CE7LoadingScene()
{
    Stop() ;
}

// 循环.
void CE7LoadingScene::step(float dt)
{
//    printf("LOADING SCENE: %f , %f\n", m_fTotalTime, dt) ;
    m_fTotalTime += dt ;

    if (IsPad())
    {
        if (m_bIsFlag)
        {
            if (2.0f <= m_fTotalTime)
            {
                m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, 1024, 768)) ;
                Stop() ;
                return ;
            }
            m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, (m_fTotalTime/2.0f)*1024, 768)) ;
        }
        else
        {
            if (STAGE_COUNT <= s_pLogic->GetStageCount())
            {
                m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, 1024, 768)) ;
                Stop() ;
                return ;
            }
            s_pLogic->CreateMap() ;
            m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, ((float)s_pLogic->GetStageCount()/(float)STAGE_COUNT)*1024, 768)) ;
        }
    }
    else // iphone.
    {
        if (m_bIsFlag)
        {
            if (2.0f <= m_fTotalTime)
            {
                m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, 480, 320)) ;
                Stop() ;
                return ;
            }
            m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, (m_fTotalTime/2.0f)*480, 320)) ;
        }
        else
        {
            if (STAGE_COUNT <= s_pLogic->GetStageCount())
            {
                m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, 480, 320)) ;
                Stop() ;
                return ;
            }
            s_pLogic->CreateMap() ;
            m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, ((float)s_pLogic->GetStageCount()/(float)STAGE_COUNT)*480, 320)) ;
        }
    }
}

void CE7LoadingScene::Start()
{
//    printf("enter loading scene - s\n") ;

    if (S_START == m_eStep)
    {
        return ;
    }

    if (IsPad())
    {
        m_eStep = S_START ;
        m_fTotalTime = ZERO_FLOAT ;
        m_bIsVisible = true ;
        
        m_pBackSprite[0] = CCSprite::spriteWithFile(loading_back_n) ;
        m_pBackSprite[0]->setAnchorPoint(CCPointZero) ;
        
        m_pBackSprite[1] = CCSprite::spriteWithFile(loading_back_s) ;
        m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, 0, 768)) ;
        m_pBackSprite[1]->setAnchorPoint(CCPointZero) ;
        
        addChild(m_pBackSprite[0]) ;
        addChild(m_pBackSprite[1]) ;
        
        m_bIsFlag = false ;
        if (STAGE_COUNT <= s_pLogic->GetStageCount())
        {
            m_bIsFlag = true ;
        }
    }
    else
    {
        m_eStep = S_START ;
        m_fTotalTime = ZERO_FLOAT ;
        m_bIsVisible = true ;
        
        m_pBackSprite[0] = CCSprite::spriteWithFile(loading_back_n_iphone) ;
        m_pBackSprite[0]->setAnchorPoint(CCPointZero) ;

        m_pBackSprite[1] = CCSprite::spriteWithFile(loading_back_s_iphone) ;
        m_pBackSprite[1]->setTextureRect(CCRectMake(0, 0, 0, 320)) ;
        m_pBackSprite[1]->setAnchorPoint(CCPointZero) ;
        
        addChild(m_pBackSprite[0]) ;
        addChild(m_pBackSprite[1]) ;
    
        m_bIsFlag = false ;
        if (STAGE_COUNT <= s_pLogic->GetStageCount())
        {
            m_bIsFlag = true ;
        }
    }

    schedule(schedule_selector(CE7LoadingScene::step)) ;
}

void CE7LoadingScene::Stop()
{
    if (S_START != m_eStep)
    {
        return ;
    }
    
    m_eStep = S_STOP ;

    unscheduleAllSelectors() ;
    removeAllChildrenWithCleanup(false) ;

    m_fTotalTime = ZERO_FLOAT ;
    m_bIsVisible = false ;

    g_pGameManager->ChangeSceneState(SS_MENU) ;

//    printf("change to menu scene\n") ;
}











